package com.hxd.mall.widget

import android.content.Context
import android.text.Editable
import android.text.InputFilter
import android.text.InputType
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.hxd.mall.R
import kotlinx.android.synthetic.main.widget_input_view_layout.view.*

/**
 * Create by yinzhengwei on 2018/11/8
 * @Function
 */
class HxdEditTextView : LinearLayout {

    private var inputType: String
    private var inputMin = 0
    private var inputMax=Int.MAX_VALUE

    private lateinit var callback: (Boolean) -> Unit

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)

    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {
        /*
         * Look the appearance up without checking first if it exists because
         * almost every TextView has one and it greatly simplifies the logic
         * to be able to parse the appearance first and then let specific tags
         * for this View override it.
         */
        context.theme.obtainStyledAttributes(
            attributeSet,
            R.styleable.HxdEditTextView, defStyleAttr, 0
        ).run {
            inputType = getString(R.styleable.HxdEditTextView_inputType) ?: "phone"
            inputMin = getInt(R.styleable.HxdEditTextView_inputMin,0)
            inputMax = getInt(R.styleable.HxdEditTextView_inputMax, Int.MAX_VALUE)
            recycle()
        }
        initView()
    }

    private fun initView() {
        LayoutInflater.from(context).inflate(R.layout.widget_input_view_layout, this)
        initInput()
        registerClick()
        focusChange()
        textChange()
    }

    private fun initInput() {
        when (inputType) {
            "phone" -> {
                input.run {
                    hint = context.getString(R.string.input_mobile_num)
                    filters = arrayOf<InputFilter>(InputFilter.LengthFilter(11))
                    inputType = InputType.TYPE_CLASS_PHONE
                }
            }
            else -> {
                input.run {
                    hint = context.getString(R.string.input_invite_code)
                    filters = arrayOf<InputFilter>(InputFilter.LengthFilter(inputMax))
                    inputType = InputType.TYPE_CLASS_TEXT
                }
            }
        }
    }

    private fun registerClick() {
        clear.setOnClickListener {
            input.text.clear()
        }
    }

    private fun textChange() {
        input.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {}

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                if ("phone" == inputType) {
                    when (s.length) {
                        0 -> {
                            clear.visibility = View.INVISIBLE
                        }
                        else -> {
                            clear.visibility = View.VISIBLE
                        }
                    }

                    if (s.length < 11) {
                        callback(false)
                    } else {
                        callback(true)
                    }
                } else {
                    if (s.length < inputMin) {
                        callback(false)
                    } else {
                        callback(true)
                    }
                }
            }
        })
    }

    private fun focusChange() {
        input.setOnFocusChangeListener { _, hasFocus ->
            if (hasFocus) {
                underLine.setBackgroundColor(resources.getColor(R.color.color_E24840))
            } else {
                underLine.setBackgroundColor(resources.getColor(R.color.color_CED3DA))
            }
        }
    }

    /**
     * 输入是否完成回调
     */
    fun inputFinish(callback: (Boolean) -> Unit) {
        this.callback = callback
    }

    fun getInputContent(): String = input.text.toString()
}